package com.txunda.zhuohired.aty.user.my;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ants.theantsgo.util.JSONUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.http.pst.InfoPst;

import java.util.Map;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/8 15:25
 * 功能描述：提现
 * 联系方式：jiahuanchen@126.com
 */

public class DepositAty extends BaseAty {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_white_center)
    private TextView tv_layout_white_center;
    /**
     * 提现方式
     */
    @ViewInject(R.id.tv_deposit_type)
    private TextView tv_deposit_type;
    /**
     * 全部提现
     */
    @ViewInject(R.id.tv_deposit_all)
    private TextView tv_deposit_all;
    /**
     * 金额
     */
    @ViewInject(R.id.et_deposit_price)
    private EditText et_deposit_price;

    @ViewInject(R.id.tv_deposit_balance)
    private TextView tv_deposit_balance;

    private int REQUESTCODE = 1;

    private String token = "";
    private InfoPst infoPst;
    private String price = "";//提现金额
    private String bank_id = "";
    private String balance = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_white_head);
        tv_layout_white_center.setText("提现");
    }

    @Override
    @OnClick({R.id.ll_deposit_type, R.id.tv_deposit_sure, R.id.tv_deposit_all})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_deposit_type://提现方式
                Bundle bundle = new Bundle();
                bundle.putString("bank_type", "2");
                startActivityForResult(MyBankAty.class, bundle, REQUESTCODE);
                break;
            case R.id.tv_deposit_sure://提现
                price = et_deposit_price.getText().toString().trim();
                String deposit_type = tv_deposit_type.getText().toString().trim();
                if (TextUtils.isEmpty(price)) {
                    showErrorTip("请输入提现金额");
                    return;
                }
                if (TextUtils.isEmpty(deposit_type)) {
                    showErrorTip("请选择提现银行卡");
                    return;
                }
                infoPst.balanceWithdraw(token, price, bank_id);
                break;
            case R.id.tv_deposit_all://全部提现
                et_deposit_price.setText(balance);
                closeKeyboard(this,tv_deposit_all);
                break;
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_deposit;
    }

    @Override
    protected void initialized() {
        token = application.getUserInfo().get("data");
        infoPst = new InfoPst(this);
    }

    @Override
    protected void requestData() {
        infoPst.wallet(token);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode) {
            if (requestCode == REQUESTCODE) {//银行卡类型
                //                L.e("========", data.getStringExtra("bank_name"));
                //                L.e("========", data.getStringExtra("bank_num"));
                //                L.e("========", data.getStringExtra("bank_id"));
                tv_deposit_type.setText(data.getStringExtra("bank_name") + "   尾号" + data.getStringExtra("bank_num")
                        .substring
                                (data.getStringExtra("bank_num").length() - 4, data.getStringExtra("bank_num").length
                                        ()));
                bank_id = data.getStringExtra("bank_id");

            }
        }
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("wallet")) {
            //            L.e("==========wallet==========", jsonStr);
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            tv_deposit_balance.setText("当前余额" + map.get("data") + "元,");
            balance = map.get("data");
            return;
        }
        if (requestUrl.contains("balanceWithdraw")) {
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            showRightTip(map.get("message"));
            finish();
            startActivity(DepositSureAty.class, null);
            return;
        }
    }
}
